package cse3311.calendarapp.gui;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import cse3311.calendarapp.DayViewController;
import cse3311.calendarapp.EventData;
import cse3311.calendarapp.R;

public class DayViewActivity extends Activity implements OnClickListener {
	private GregorianCalendar date;
	private boolean[] selectedTimeSlots_m = new boolean[24];
	private Drawable defaultButtonBg_m;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_day_view);

		// get the date from the intent which was passed by MainActivity
		date = (GregorianCalendar) getIntent().getExtras().getSerializable(
				"date");

		final ActionBar actionBar = getActionBar();
		final String dateFormatString = "EEEE, MMMM d, yyyy";
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString,
				Locale.getDefault());
		String title = dateFormat.format(date.getTime());

		actionBar.setTitle(title);
	}

	@Override
	protected void onStart() {
		super.onStart();

		EventData[] data = DayViewController.getEvents(date);

		LinearLayout dayView = (LinearLayout) findViewById(R.id.day_view);
		dayView.removeAllViews();

		for (int i = 0; i < 24; ++i) { // hardcoded 24 hours, could change
			Button button = new Button(getApplicationContext());

			int hour = i;

			String text = "";
			if (hour < 12) {
				if (hour == 0)
					hour = 12;
				text = "" + hour + ":00 AM";
			} else {
				hour -= 12;
				if (hour == 0)
					hour = 12;
				text = "" + hour + ":00 PM";
			}

			if (data[i] != null) {
				text += " " + data[i].alias_m;
			}

			GregorianCalendar eventDate = new GregorianCalendar(
					date.get(GregorianCalendar.YEAR),
					date.get(GregorianCalendar.MONTH),
					date.get(GregorianCalendar.DAY_OF_MONTH), i, 0);
			// eventDate.set(GregorianCalendar.YEAR,
			// date.get(GregorianCalendar.YEAR));
			// eventDate.set(GregorianCalendar.MONTH,
			// date.get(GregorianCalendar.MONTH));
			// eventDate.set(GregorianCalendar.DAY_OF_MONTH,
			// date.get(GregorianCalendar.DAY_OF_MONTH));
			// eventDate.set(GregorianCalendar.HOUR_OF_DAY, i);

			button.setText(text);
			button.setTextColor(Color.BLACK);
			button.setGravity(Gravity.LEFT);
			button.setOnClickListener(this);
			button.setTag(R.id.event_date, eventDate);
			button.setTag(R.id.event_data, data[i]);

			if (selectedTimeSlots_m[i]) {
				button.setBackgroundColor(Color.GREEN);
			} else {
				button.setBackgroundResource(android.R.drawable.btn_default);
			}
			dayView.addView(button);
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		Bundle bundle = getIntent().getExtras();

		MenuItem scheduleGroupEvent = menu
				.findItem(R.id.action_schedule_group_event);
		MenuItem nextGroupEvent = menu.findItem(R.id.action_next_group_event);
		scheduleGroupEvent.setVisible(!bundle.getBoolean("groupEvent"));
		nextGroupEvent.setVisible(bundle.getBoolean("groupEvent"));

		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.day_view, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_schedule_group_event:
			Intent intent = new Intent(this, DayViewActivity.class);
			intent.putExtra("groupEvent", true);
			intent.putExtra("date", date);
			startActivity(intent);
			break;

		case R.id.action_next_group_event:
			boolean slotsSelected = false;

			for (int i = 0; i < 24; i++) {
				slotsSelected |= selectedTimeSlots_m[i];
			}

			if (!slotsSelected) {
				break;
			}

			Intent groupEventIntent = new Intent(this,
					ScheduleGroupEventActivity.class);
			groupEventIntent.putExtra("selectedTimeSlots", selectedTimeSlots_m);
			groupEventIntent.putExtra("date", date);
			startActivity(groupEventIntent);
			finish();
			break;

		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}

	@Override
	public void onClick(View view) {
		Bundle bundle = getIntent().getExtras();

		GregorianCalendar date = (GregorianCalendar) view
				.getTag(R.id.event_date);
		int hour = date.get(GregorianCalendar.HOUR_OF_DAY);

		EventData data = (EventData) view.getTag(R.id.event_data);

		if (bundle.getBoolean("groupEvent")) {
			if (!selectedTimeSlots_m[hour] && data == null) {
				view.setBackgroundColor(Color.GREEN);
			} else {
				view.setBackgroundResource(android.R.drawable.btn_default);
			}

			if (data == null) {
				selectedTimeSlots_m[hour] = !selectedTimeSlots_m[hour];
			}
		} else {
			Intent intent = new Intent(this, EditEventActivity.class);
			intent.putExtra("date", date);
			intent.putExtra("data", data);
			startActivity(intent);
		}
	}
}
